﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Web;
using System.Xml.Linq;
using System.Globalization;

namespace TheOutfield.UmbExt.UniversalMediaPicker.Providers.SlideShare
{
    public static class SideshareExtensions
    {
        #region Internal

        internal static Slideshow ToSlideshow(this XElement slideshow)
        {
            var dateFormat = "ddd MMM dd HH:mm:ss K yyyy";

            DateTime created;
            DateTime updated;

            return new Slideshow
            {
                Id = slideshow.Element("ID") != null ? Convert.ToInt32(slideshow.Element("ID").Value) : 0,
                Title = slideshow.Element("Title") != null ? slideshow.Element("Title").Value : "",
                Description = slideshow.Element("Description") != null ? slideshow.Element("Description").Value : "",
                Status = slideshow.Element("Status") != null ? (SlideshowStatus)Enum.Parse(typeof(SlideshowStatus), slideshow.Element("Status").Value) : SlideshowStatus.Queued,
                Username = slideshow.Element("Username") != null ? slideshow.Element("Username").Value : "",
                Url = slideshow.Element("Url") != null ? slideshow.Element("Url").Value : "",
                ThumbnailUrl = slideshow.Element("ThumbnailURL") != null ? slideshow.Element("ThumbnailURL").Value : "",
                ThumbnailSmallUrl = slideshow.Element("ThumbnailSmallURL") != null ? slideshow.Element("ThumbnailSmallURL").Value : "",
                EmbedCode = slideshow.Element("Embed") != null ? slideshow.Element("Embed").Value : "",
                Created = slideshow.Element("Created") != null && DateTime.TryParseExact(slideshow.Element("Created").Value, dateFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out created) ? created : DateTime.Now,
                Updated = slideshow.Element("Updated") != null && DateTime.TryParseExact(slideshow.Element("Updated").Value, dateFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out updated) ? updated : DateTime.Now,
                Language = slideshow.Element("Language") != null ? slideshow.Element("Language").Value : "",
                Format = slideshow.Element("Format") != null ? slideshow.Element("Format").Value : "",
                Download = slideshow.Element("Download") != null && slideshow.Element("Download").Value == "1",
                DownloadUrl = slideshow.Element("DownloadUrl") != null ? slideshow.Element("DownloadUrl").Value : "",
                Type = slideshow.Element("SlideshowType") != null ? (SlideshowType)Enum.Parse(typeof(SlideshowType), slideshow.Element("SlideshowType").Value) : SlideshowType.Presentation
            };
        }

        internal static string UrlEncode(this string input)
        {
            return input == null ? input : HttpUtility.UrlEncode(input).Replace("+", "%20");
        }

        internal static string CalculateSHA1(this string text, Encoding enc)
        {
            byte[] buffer = enc.GetBytes(text);
            var cryptoTransformSHA1 = new SHA1CryptoServiceProvider();
            return BitConverter.ToString(cryptoTransformSHA1.ComputeHash(buffer)).Replace("-", "").ToLower();
        }

        #endregion

        #region Public

        public static MediaItem ToMediaItem(this Slideshow slideshow)
        {
            var mi = new MediaItem
            {
                Id = slideshow.Id.ToString(),
                Title = slideshow.Title,
                Icon = "mediaSlideshare.gif",
                HasChildren = false,
                Selectable = true,
                PreviewImageUrl = slideshow.ThumbnailSmallUrl
            };

            mi.MetaData.Add("Description", slideshow.Description);
            mi.MetaData.Add("Created", slideshow.Created.ToShortDateString() + " " + slideshow.Created.ToShortTimeString());
            mi.MetaData.Add("Updated", slideshow.Updated.ToShortDateString() + " " + slideshow.Updated.ToShortTimeString());

            return mi;
        }

        #endregion
    }
}